package cn.milk.servlet;

import cn.milk.dao.MenuDAO;
import cn.milk.dao.OrderDAO;
import cn.milk.dao.UserDAO;
import cn.milk.model.Order;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/OrderServlet")
public class OrderServlet extends HttpServlet {
    private OrderDAO orderDAO = new OrderDAO();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        String search = request.getParameter("search") != null ? request.getParameter("search") : "";
        int page = request.getParameter("page") != null ? Integer.parseInt(request.getParameter("page")) : 1;
        int pageSize = 3;

        try {
            if ("delete".equals(action)) {
                int id = Integer.parseInt(request.getParameter("id"));
                orderDAO.deleteOrder(id);
                response.sendRedirect("OrderServlet");
                return;
            }

            if ("edit".equals(action)) {
                int id = Integer.parseInt(request.getParameter("id"));
                Order order = orderDAO.getOrderById(id);
                request.setAttribute("order", order);

                // 获取用户和菜单列表
                UserDAO userDAO = new UserDAO();
                MenuDAO menuDAO = new MenuDAO();
                request.setAttribute("users", userDAO.getUsers());
                request.setAttribute("menus", menuDAO.getMenus());

                request.getRequestDispatcher("editorders.jsp").forward(request, response);
                return;
            }

            int totalCount = orderDAO.getTotalCount(search);
            int totalPages = (int) Math.ceil((double) totalCount / pageSize);
            List<Order> orders = orderDAO.getOrders(search, page, pageSize);

            request.setAttribute("orders", orders);
            request.setAttribute("totalPages", totalPages);
            request.setAttribute("currentPage", page);
            request.getRequestDispatcher("ordersmanage.jsp").forward(request, response);
        } catch (SQLException e) {
            throw new ServletException(e);
        }
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        try {
            UserDAO userDAO = new UserDAO();
            MenuDAO menuDAO = new MenuDAO();

            if ("add".equals(action)) {
                int userid = Integer.parseInt(request.getParameter("userid"));
                String username = userDAO.getUsernameById(userid);
                int menuid = Integer.parseInt(request.getParameter("menuid"));
                String menuname = menuDAO.getMenunameById(menuid);
                int count = Integer.parseInt(request.getParameter("count"));
                String amount = String.valueOf(count * menuDAO.getPriceById(menuid));
                int status = Integer.parseInt(request.getParameter("status"));

                Order order = new Order();
                order.setUserid(userid);
                order.setUsername(username);
                order.setMenuid(menuid);
                order.setMenuname(menuname);
                order.setCount(count);
                order.setAmount(amount);
                order.setStatus(status);
                orderDAO.addOrder(order);
            } else if ("edit".equals(action)) {
                int id = Integer.parseInt(request.getParameter("id"));
                int userid = Integer.parseInt(request.getParameter("userid"));
                String username = userDAO.getUsernameById(userid);
                int menuid = Integer.parseInt(request.getParameter("menuid"));
                String menuname = menuDAO.getMenunameById(menuid);
                int count = Integer.parseInt(request.getParameter("count"));
                String amount = String.valueOf(count * menuDAO.getPriceById(menuid));
                int status = Integer.parseInt(request.getParameter("status"));

                Order order = new Order();
                order.setId(id);
                order.setUserid(userid);
                order.setUsername(username);
                order.setMenuid(menuid);
                order.setMenuname(menuname);
                order.setCount(count);
                order.setAmount(amount);
                order.setStatus(status);
                orderDAO.updateOrder(order);
            }else if ("confirmPayment".equals(action)) {
                int id = Integer.parseInt(request.getParameter("id"));
                orderDAO.updateOrderStatus(id, 2); // 更新状态为 2 (已付款)
                response.setStatus(200); // 返回成功状态
            }
            response.sendRedirect("OrderServlet");
        } catch (SQLException e) {
            throw new ServletException(e);
        }
    }

}
